#include <iostream>
#include <string>
#include <cstring>
#include <vector>

using std::cout;
using std::endl;
using std::string;
using std::vector;

size_t split(const string &str, const string &sep, vector<string> &result)
{
    // 对str根据sep进行分割，分割加过保存在result中
    int cnt = 0;
    size_t start = 0;
    while (start < str.size())
    {
        ssize_t pos = str.find(sep, start);
        if (pos == string::npos)
        {
            string tmp = str.substr(start);
            if (!tmp.empty())
            {
                result.push_back(tmp);
                cnt++;
            }
            break;
        }
        if (start == pos) // 说明有两个分隔符连在一起，且中间没有数据
        {
            start += sep.size();
            continue;
        }
        result.push_back(str.substr(start, pos - start));
        cnt++;
        start = pos + sep.size();
    }
    return cnt;
}

int main()
{
    string str = "abc\r\nusaih\r\nosuh\r\nshaiu\r\n";
    vector<string> ret;
    size_t n = split(str, "\r\n", ret);
    cout << "n:" << n << endl;
    for (const auto &s : ret)
    {
        cout << s << endl;
    }

    return 0;
}